package com.yisu.expressway.manager.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.yisu.expressway.ewf.entity.GoodsArchives;
import com.yisu.expressway.ewf.entity.GoodsCategory;
import com.yisu.expressway.ewf.entity.RecommendGoods;
import com.yisu.expressway.manager.core.mapper.GoodsArchivesMapper;
import com.yisu.expressway.manager.core.mapper.GoodsCategoryMapper;
import com.yisu.expressway.manager.core.mapper.RecommendGoodsMapper;
import com.yisu.expressway.manager.core.service.IRecommendGoodsService;
import com.yisu.expressway.manager.domain.util.JedisClusterUtil;
import com.yisu.expressway.manager.domain.util.RedisUtil;
import com.yisu.expressway.manager.domain.vo.RecommendGoodsForSelVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xlt on 2017/4/27.
 */
@Service
public class RecommendGoodsServiceImpl implements IRecommendGoodsService {
    @Resource
    private RecommendGoodsMapper recommendGoodsMapper;
    @Resource
    private GoodsArchivesMapper goodsArchivesMapper;

    /**
     * 批量添加类目推荐商品
     *
     * @param recommendGoodses 类目推荐商品
     * @return
     */
    @Override
    public boolean batchAddRecommendGoods(List<RecommendGoods> recommendGoodses) {
        try {
            if (!CollectionUtils.isEmpty(recommendGoodses)) {
                for (RecommendGoods item : recommendGoodses) {
                    Integer maxSort = recommendGoodsMapper.maxSortBySourceCode(item.getSourceCode());
                    item.setSort(maxSort + 1);
                    if (recommendGoodsMapper.isExitByInfo(item) <= 0) {
                        recommendGoodsMapper.insert(item);
                    }
                }
                return true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * 修改类目推荐商品排序
     *
     * @param recommendGoods 类目推荐商品信息
     * @return 成功，失败
     */
    @Override
    public boolean updateSort(RecommendGoods recommendGoods) {
        return recommendGoodsMapper.updateSort(recommendGoods) > 0;
    }

    /**
     * 删除推荐商品通过主键信息
     *
     * @param primaryKey 主键信息
     * @return 成功，失败
     */
    @Override
    public boolean deleteRecommendGoodsByPrimaryKey(Long primaryKey) {
        return recommendGoodsMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    /**
     * 通过分类获取分类推荐商品列表
     *
     * @param categoryCode 分类编号
     * @return 分类列表
     */
    @Override
    public List<RecommendGoodsForSelVo> getRecommendGoodsGoodsArchivesByCategoryCode(String categoryCode) {
        List<RecommendGoodsForSelVo> recommendGoodsForSelVos = Lists.newArrayList();
        recommendGoodsForSelVos = goodsArchivesMapper.getRecommendGoodsGoodsArchivesByCategoryCode(categoryCode);
      /*  String jsonObj = JedisClusterUtil.getInstance().strings().get("recommendGoodsForSelVos");
        if (!StringUtils.isEmpty(jsonObj)) {
            recommendGoodsForSelVos = (List<RecommendGoodsForSelVo>) JSON.parse(jsonObj);
            if (CollectionUtils.isEmpty(recommendGoodsForSelVos)) {
                JedisClusterUtil.getInstance().keys().del("recommendGoodsForSelVos");
            }
        } else {
            recommendGoodsForSelVos = goodsArchivesMapper.getRecommendGoodsGoodsArchivesByCategoryCode(categoryCode);
            JedisClusterUtil.getInstance().strings().set("recommendGoodsForSelVos", JSON.toJSONString(recommendGoodsForSelVos));
        }*/
        return recommendGoodsForSelVos;
    }
}
